$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/bplatformcommodityinfo/list',
        datatype: "json",
        colModel: [			
			{ label: '商品名称', name: 'id', index: 'id',align: "center", width: 50, key: true ,formatter:function(value,options,rowData){
				var name=rowData["commodityName"];
					return "<a onclick=update("+value+") >"+name+"</a>";
				}
			},
			{ label: '品牌', name: 'brandName', index: 'brandName',align: "center", width: 80 },
			{ label: '商品图片', name: 'commodityImgUrl', index: 'commodity_img_url',align: "center", width: 80 ,sortable: false, editable: false,formatter:function(value,options,rowData){
					return "<img src='"+value+"' width='60'/>"
				}
			},
			{ label: '商品描述', name: 'commodityDesc', index: 'commodity_desc',align: "center", width: 80 }, 			
			{ label: '商品状态', name: 'commodityStatus', index: 'commodity_status',align: "center", width: 80 },
			{ label: '商品价格', name: 'commodityPrice', index: 'commodity_price',align: "center", width: 80 }, 			
			{ label: '商品原价', name: 'originalCommodityPrice', index: 'original_commodity_Price',align: "center", width: 80 }, 			
			{ label: '创建时间', name: 'createTime', index: 'create_time',align: "center", width: 80 }, 			
			{ label: '修改时间', name: 'modifyTime', index: 'modify_time',align: "center", width: 80 },
			{ label: '操作', name: 'commodityStatus', index: 'commodity_status',align: "center", width: 80,formatter:function(value,options,rowData){
					if(value==0){
						return "<a>"+'恢复'+"</a>";
					}else{
						return "<a>"+'删除'+"</a>";
					}
				}
			},
        ],
		viewrecords: true,
        height: 520,
        rowNum: 15,
		rowList : [15,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});
setTimeout(function () {
	$.ajax({
		type: "POST",
		async:false,
		url: baseURL + 'sys/bbrandinfo/queryAll',
		contentType: "application/json",
		success: function(r){
			console.log(r);
			if(r.code==0){
				for(var i=0;i<r.brandInfo.length;i++){
					$("#brandName").append(
						"<option value=" + r.brandInfo[i].brandName +">"+"</option>");
					$("#brand").append(
						"<option value=" + r.brandInfo[i].brandName +">"+"</option>");
				}
			}
		}
	});
},2000)
function update(e) {
	vm.showList = false;
	vm.title = "修改";

	vm.getInfo(e)
}
var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		bPlatformCommodityInfo: {},
		brandName:""
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.bPlatformCommodityInfo = {};
			$("#img").attr("src",'/lkt-admin/statics/images/sys/timg.jpg');
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
			$('.uploadImg').hide();
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			vm.bPlatformCommodityInfo.brandName=vm.brandName;
			var url = vm.bPlatformCommodityInfo.id == null ? "sys/bplatformcommodityinfo/save" : "sys/bplatformcommodityinfo/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.bPlatformCommodityInfo),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
							$('#img').clear();
						});
					}else{
						alert(r.msg);
						$('#img').clear();
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/bplatformcommodityinfo/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "sys/bplatformcommodityinfo/info/"+id, function(r){
                vm.bPlatformCommodityInfo = r.bPlatformCommodityInfo;
                vm.brandName=r.brandName;
				$('.uploadImg-img').attr('src',vm.bPlatformCommodityInfo.commodityImgUrl);
				$('.uploadImgno').hide();
				$('.uploadImg').show();
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
			$('#img').clear();
		}
	}
});